<?php

class Admin_IndexController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    public function indexAction() {
        
    }

    public function profileAction() {
        
        //Initialize
        $successmessage = "";
        $errorMessageArray = Array();
        $data = array();
        $urlData = Array();
        $this->view->mode = "Add";
        $currentHashTag = '';
        $lgn_id = $this->USER->lgn_id;
        $urlModel = new Model_Url();
        $meetingForm = new Form_Admin_Profile();
        
        
        
        //Load login Model
        $loginModel = new Model_Login();
        
        
        
        if (!empty($url_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $urlModel->fetchEntryById($url_id);
            $currentHashTag = $data['url_hashtag'];
            //check the Validation the meeting created is of the same user
            if ($data) {
                if ($data['url_lgn_id'] != $lgn_id) {
                    $this->_helper->redirector('list', 'meeting', 'admin');
                }
            }
        }

        //Populate form 
        $this->view->form = $meetingForm->populate($data);

        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if (!$meetingForm->isValid($postedData)) {
                $errorMessage = $meetingForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
                //Check for the email exist while Adding custoemr

                if ($this->view->mode == "Add") {
                    $hashtagExist = $urlModel->checkHashTagExist($postedData['url_hashtag']);
                    if ($hashtagExist) {
                        $errorMessageArray[] = "Custom URL already exists! Try another";
                    }
                } else {

                    //Check for the hasttag exist while Editing meeting
                    $hashtagExist = $urlModel->checkHashTag($currentHashTag, $postedData['url_hashtag']);
                    if (!$hashtagExist) {
                        $errorMessageArray[] = "Custom URL already exists! Try another";
                    }
                }



                if (count($errorMessageArray) > 0) {
                    
                } else {

                    $urlData['url_id'] = $url_id;
                    $urlData['url_lgn_id'] = $lgn_id;
                    $urlData['url_name'] = $postedData['url_name'];
                    $urlData['url_hashtag'] = $postedData['url_hashtag'];
                    $urlData['url_askpage_url'] = $postedData['url_askpage_url'];
                    $urlData['url_presentatioin_url'] = $postedData['url_presentatioin_url'];
                    $urlData['url_status'] = $postedData['url_status'];

                    $urlModel->save($urlData, 'url_id');
                    if ($this->view->mode == "Add") {
                        $successmessage = "Meeting added successfully";
                    } else {
                        $successmessage = "Meeting updated successfully";
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'meeting', 'admin');
                }
            }
        }

        $this->view->form = $meetingForm;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
    }

}

?>
